package com.bdsys.daoimpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bdsys.common.dao.BasicDaoAbstract;
import com.bdsys.dao.UserDAO;
import com.bdsys.domain.UserDomain;

public class UserDAOImpl extends BasicDaoAbstract implements UserDAO{

	public UserDAOImpl(){
		super("user");
	}
	
	@Override
	public void addUser(UserDomain userDomain) {
		getJdbcTemplate().update(getSql(USER_INSERT),
				new Object[] { userDomain.getuClass(), userDomain.getPassword(),
			userDomain.getEmail(), userDomain.getPhone(), userDomain.getType(),userDomain.getId(),userDomain.getName() });
	}

	@Override
	public List<UserDomain> selectUsersByConditions(UserDomain inDomain) {
		StringBuilder sb = new StringBuilder(getSql(SEARCH_USER_BY_CONDITIONS));
		List<String> list = new ArrayList<String>();
		boolean isWhere = false;
		if((inDomain.getuClass() != null && !inDomain.getuClass().isEmpty())){
			sb.append(" where u.class = ?");
			isWhere = true;
			list.add(inDomain.getuClass());
		}
		
		if ((inDomain.getName() != null && !inDomain.getName().isEmpty())){
			if(isWhere){
				sb.append(" and u.name = ?");
			}else {
				sb.append(" where u.name = ?");
				
				isWhere = true;
			}
			
			list.add(inDomain.getName());
		}
		
		if ((inDomain.getPassword() != null && !inDomain.getPassword().isEmpty())){
			if(isWhere){
				sb.append(" and u.password = ?");
			}else {
				sb.append(" where u.password = ?");
				
				isWhere = true;
			}
			
			list.add(inDomain.getPassword());
		}
		
		if ((inDomain.getEmail() != null && !inDomain.getEmail().isEmpty())){
			if(isWhere){
				sb.append(" and u.email = ?");
			}else {
				sb.append(" where u.email = ?");
				
				isWhere = true;
			}
			
			list.add(inDomain.getEmail());
		}
		
		if ((inDomain.getPhone() != null && !inDomain.getPhone().isEmpty())){
			if(isWhere){
				sb.append(" and u.phone = ?");
			}else {
				sb.append(" where u.phone = ?");
			}
			
			list.add(inDomain.getPhone());
		}
		
		if ((inDomain.getType() != null && !inDomain.getType().isEmpty())){
			if(isWhere){
				sb.append(" and u.type = ?");
			}else {
				sb.append(" where u.type = ?");
			}
			
			list.add(inDomain.getType());
		}
		
		if ((inDomain.getId() != null && !inDomain.getId().isEmpty())){
			if(isWhere){
				sb.append(" and u.id = ?");
			}else {
				sb.append(" where u.id = ?");
			}
			
			list.add(inDomain.getId());
		}
	
		List<Map<String, Object>> resultList =  getJdbcTemplate().queryForList(sb.toString(), list.toArray());
		
		List<UserDomain> users = new ArrayList<UserDomain>();
		for(Map<String, Object> map : resultList){
			UserDomain user = this.fromMapToDomain(map);
			users.add(user);
		}
		return users;
	}

	@Override
	public void addUserType(List<String> userTypes) {
		for(String userType : userTypes){
			if(userType != null && !userType.isEmpty()){
				getJdbcTemplate().update(getSql(USER_TYPE_INSERT),
						new Object[] { userType});
			}
		}
	}

	@Override
	public Map<String, String> selectAllUserType() {
		List<Map<String, Object>> list =  getJdbcTemplate().queryForList(getSql(SEARCH_ALL_USER_TYPE), new Object[]{});
		Map<String, String> userTypes = new HashMap<String, String>();
		for(Map<String, Object> map : list){
			userTypes.put(String.valueOf(map.get("id")), String.valueOf(String.valueOf(map.get("type"))));
		}
		return userTypes;
	}

	@Override
	public void deleteUserType(String id) {
		getJdbcTemplate().update(getSql(USER_TYPE_DELETE),
				new Object[] { id});
		
	}

	@Override
	public String checkLoginUser(String name, String password) {
		List<Map<String,Object>> list = getJdbcTemplate().queryForList(getSql(CHECK_LOGIN_USER), new Object[]{name,password});
		String type=null;
		for(Map<String, Object> map:list){
			type=String.valueOf(map.get("type"));
		}
		return type;
	}
	
	public UserDomain fromMapToDomain(Map<String, Object> map){
		UserDomain domain = new UserDomain();
		domain.setId(String.valueOf(map.get("id")));
		domain.setName(String.valueOf(map.get("name")));
		domain.setType(String.valueOf(map.get("typeId")));
		domain.setPhone(String.valueOf(map.get("phone")));
		domain.setEmail(String.valueOf(map.get("email")));
		domain.setPassword(String.valueOf(map.get("password")));
		domain.setuClass(String.valueOf(map.get("class")));
		domain.setTypeName(String.valueOf(map.get("type")));
		return domain;
	}

	@Override
	public void deleteUser(String uid) {
		getJdbcTemplate().update(getSql(USER_DELETE), new Object[]{uid});
	}
}
